﻿using d1Common;
using d1Common.WebController;
using d1Project.Modules.Common.do4A.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace d1Project.Modules.Common.do4A.Domain
{
    public class VerificationCodeMgr
    {
        private static VerificationCodeMgr singleInstance = new VerificationCodeMgr();
        public static VerificationCodeMgr Instance
        {
            get
            {
                return singleInstance;
            }
        }
        public VerificationCodeMgr()
        {

        }

        public VerificationCode GetVerficationCode(int length, int _timeSpan)
        {
            do4ADbContext db = new do4ADbContext();
            try
            {
                string _code = "";
                StringBuilder _StrB = new StringBuilder();
                Random _random = new Random();
                for (int i = 0; i < length; i++)
                {
                    _StrB.Append(_random.Next(10).ToString());
                }
                _code = _StrB.ToString();
                VerificationCode _verificationCode = new VerificationCode();
                _verificationCode.Id = doServices.Instance.DoCore.getGUID();
                _verificationCode.Code = _code;
                _verificationCode.ErrorCount = 0;
                _verificationCode.CreateTime = DateTime.Now;
                if (_timeSpan < 0)
                {
                    _verificationCode.InvalidTime = DateTime.MaxValue;
                }
                else
                {
                    _verificationCode.InvalidTime = DateTime.Now.AddMinutes(_timeSpan);
                }
                db.VerificationCodes.Add(_verificationCode);
                db.SaveChanges();
                return _verificationCode;
            }
            finally
            {

                db.Dispose();
            }
        }

        public void checkVerificationCode(do4ADbContext db, string _id, string _code, int _maxErrorCount)
        {
            VerificationCode _verificationCode = db.VerificationCodes.Where(x => x.Id == _id).FirstOrDefault();
            if (_verificationCode == null)
            {
                throw new UserException("验证码已过期");
            }
            if (DateTime.Now > _verificationCode.InvalidTime)
            {
                throw new UserException("验证码已过期");
            }
            if (_maxErrorCount >= 0 && _maxErrorCount <= _verificationCode.ErrorCount)
            {
                _verificationCode.InvalidTime = DateTime.Now;
                db.Entry(_verificationCode).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                throw new UserException("验证码已失效");
            }
            if (_verificationCode.Code.ToLower() == _code.ToLower())
            {
                db.VerificationCodes.Remove(_verificationCode);
                db.SaveChanges();
                return;
            }
            _verificationCode.ErrorCount++;
            db.Entry(_verificationCode).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            throw new Exception("验证码不正确");
        }
    }
}